package org.komarichin.services;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.komarichin.beans.IArticle;
import org.komarichin.beans.INews;
import org.komarichin.beans.IPublishingEntry;
import org.komarichin.beans.dao.IArticleDAO;
import org.komarichin.beans.dao.INewsDAO;
import org.komarichin.beans.service.IOutdoorService;
import org.komarichin.feed.IEntryFeed;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.io.StringReader;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * User: Michael Komarichin aka mikom
 * Date: 24.05.2010
 * Time: 14:58:09
 */

public class OutdoorService implements IOutdoorService {

    private static final Logger log = LoggerFactory.getLogger(OutdoorService.class);

    @Autowired
    private IArticleDAO<IArticle<Long>, Long> articleDAO;
    @Autowired
    private INewsDAO<INews<Long>, Long> newsDAO;

    @Override
    public String makeBackUp() {
        StringBuilder buff = new StringBuilder("<root>");
        Collection<IArticle<Long>> articles = articleDAO.getAll();
        for (IArticle<Long> ar : articles) {
            buff.append("<article>");
            buff.append("<id>").append(ar.getId()).append("</id>");
            buff.append("<name>").append(ar.getName()).append("</name>");
            buff.append("<content>").append("<![CDATA[").append(ar.getContent()).append("]]>").append("</content>");
            buff.append("<title>").append(ar.getTitle()).append("</title>");
            buff.append("<description>").append(ar.getDescription()).append("</description>");
            buff.append("<source>").append(ar.getSource()).append("</source>");
            buff.append("<create>").append(ar.getCreate().getTime()).append("</create>");
            buff.append("<update>").append(ar.getUpdate().getTime()).append("</update>");
            buff.append("<keywords>").append(ar.getKeyWords()).append("</keywords>");
            buff.append("<language>").append(ar.getLanguage()).append("</language>");
            buff.append("<link>").append(ar.getLink()).append("</link>");
            buff.append("</article>");
        }
        Collection<INews<Long>> news = newsDAO.getAll();
        for (INews<Long> n : news) {
            buff.append("<news>");
            buff.append("<id>").append(n.getId()).append("</id>");
            buff.append("<name>").append(n.getName()).append("</name>");
            buff.append("<content>").append("<![CDATA[").append(n.getContent()).append("]]>").append("</content>");
            buff.append("<title>").append(n.getTitle()).append("</title>");
            buff.append("<description>").append(n.getDescription()).append("</description>");
            buff.append("<source>").append(n.getSource()).append("</source>");
            buff.append("<create>").append(n.getCreate().getTime()).append("</create>");
            buff.append("<update>").append(n.getUpdate().getTime()).append("</update>");
            buff.append("<keywords>").append(n.getKeyWords()).append("</keywords>");
            buff.append("<language>").append(n.getLanguage()).append("</language>");
            buff.append("<link>").append(n.getLink()).append("</link>");
            buff.append("</news>");
        }

        return buff.append("</root>").toString();
    }

    private void buildElement(IPublishingEntry<Long> obj, List<Element> els) {
        IEntryFeed ef = (IEntryFeed)obj;
        for (Element el : els) {
            if ("id".equals(el.getName())) {
                obj.setId(Long.valueOf(el.getValue()));
            }
            if ("name".equals(el.getName())) {
                obj.setName(el.getValue());
            }
            if ("content".equals(el.getName())) {
                obj.setContent(el.getValue());
            }
            if ("title".equals(el.getName())) {
                ef.setTitle(el.getValue());
            }
            if ("description".equals(el.getName())) {
                ef.setDescription(el.getValue());
            }
            if ("source".equals(el.getName())) {
                if(el instanceof INews){
                    ((INews)ef).setSource(el.getValue());
                }
                if(el instanceof IArticle){
                    ((IArticle)ef).setSource(el.getValue());
                }
            }
            if ("create".equals(el.getName())) {
                obj.setCreate(new Date(Long.valueOf(el.getValue())));
            }
            if ("update".equals(el.getName())) {
                ef.setUpdate(new Date(Long.valueOf(el.getValue())));
            }
            if ("keywords".equals(el.getName())) {
                ef.setKeyWords(el.getValue());
            }
            if ("language".equals(el.getName())) {
                obj.setLanguage(Long.valueOf(el.getValue()));
            }
            if ("link".equals(el.getName())) {
                ef.setLink(el.getValue());
            }

        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public void makeRestore(String xml) {
        SAXBuilder builder = new SAXBuilder();
        try {
            Document doc = builder.build(new StringReader(xml));
            Element root = doc.getRootElement();
            List<Element> children = root.getChildren();
            for (Element c : children) {
                List<Element> els = c.getChildren();
                if ("article".equals(c.getName())) {
                    IArticle<Long> article = articleDAO.newInstance();
                    buildElement(article, els);
                    articleDAO.saveOrUpdate(article);
                }
                if("news".equals(c.getName())){
                    INews<Long> news = newsDAO.newInstance();
                    buildElement(news, els);
                    newsDAO.saveOrUpdate(news);
                }
            }

        } catch (JDOMException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }


}
